﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace GnuPGNotepad
{
    public class Utils
    {
        #region public functions
        /// <summary>
        /// displays a formatted message box with generic messagebox title
        /// </summary>
        /// <param name="message">Message to display to user</param>
        /// <param name="messageIcon">the icon to display</param>
        public static void ShowMessage(string message, MessageBoxIcon messageIcon)
        { ShowMessage(message, Application.ProductName, messageIcon); }

        /// <summary>
        /// displays a formatted message box with custom messagebox title
        /// </summary>
        /// <param name="message">Message to display to user</param>
        /// <param name="title">Title of the message box</param>
        /// <param name="messageIcon">the icon to display</param>
        public static void ShowMessage(string message, string title, MessageBoxIcon messageIcon)
        { MessageBox.Show(message, title, MessageBoxButtons.OK, messageIcon); }

        /// <summary>
        /// A generic form used to ask for things like passwords and recipient info
        /// </summary>
        /// <param name="title">Title of the form</param>
        /// <param name="message">The message to display on the label</param>
        /// <param name="defaultContent">The default text to put into the input textbox (ex. previous setting)</param>
        /// <param name="isPassword">If input is password then use system password character</param>
        /// <param name="result">Returns the text entered into the input textbox</param>
        public static void ShowGenericForm(string title, string message, string defaultContent, bool isPassword, out string result)
        {
            result = null;

            try
            {
                using (frmGenericForm fm = new frmGenericForm())
                {
                    fm.Text = string.Format("{0} - {1}", title, Application.ProductName);
                    fm.label1.Text = message;
                    fm.textBox1.Text = defaultContent;
                    fm.textBox1.UseSystemPasswordChar = isPassword;
                    fm.textBox1.Focus();

                    if (fm.ShowDialog() == DialogResult.OK)
                        result = fm.textBox1.Text;
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Displays a non-modal form containing a simple textbox for displaying output info
        /// </summary>
        /// <param name="title">The title of the form</param>
        /// <param name="content">The content to populate the textbox</param>
        public static void ShowImmediateForm(string title, string content)
        {
            frmImmediateView fm = new frmImmediateView();
            fm.Text = string.Format("{0} - {1}", title, Application.ProductName);
            fm.textBox1.Text = content;
            fm.textBox1.Focus();
            fm.ShowInTaskbar = true;
            fm.Show();
        }

        /// <summary>
        /// Utility to convert Sting object to Stream object
        /// </summary>
        public static Stream ConvertTo(string text)
        {
            if (string.IsNullOrEmpty(text))
                return null;

            try
            {
                byte[] byteArray = Encoding.Default.GetBytes(text);
                return new MemoryStream(byteArray);
            }
            catch (Exception)
            { throw; }
        }

        /// <summary>
        /// Utility to convert Stream object to String object
        /// </summary>
        public static string ConvertTo(Stream stream)
        {
            if (null == stream)
                return null;

            try
            {
                if (!stream.CanRead)
                    throw new Exception("Unable to read stream");

                stream.Position = 0;
                return new StreamReader(stream).ReadToEnd();

            }
            catch (Exception)
            { throw; }
        }

        /// <summary>
        ///  Writes input stream into output stream using buffered response
        /// </summary>
        public static void WriteTo(Stream input, Stream output)
        {
            using (BufferedStream bufferedresponse = new BufferedStream(input))
            {
                int count = 0;
                byte[] buffer = new byte[4096];
                do
                {
                    count = bufferedresponse.Read(buffer, 0, buffer.Length);
                    output.Write(buffer, 0, count);
                } while (count > 0);
            }
        }
        
        #endregion // public functions
    }
}
